/*INIT*/
$(document).ready(function() {
	if ($('#errorCode').length != 0 && $.trim($('#errorCode').val()) != '') {
		$('#showErrorCode').html($('#errorCode').val());
	}
	formChecker.init();

});
var theCaptcha = '';
var formChecker = {
	init : function() {
		$('#form_signup [name=name]').focusout(formChecker.checkName).focusout();
		$('#form_signup [name=email]').focusout(formChecker.checkEmail);
		$('#form_signup [name=password]').focusout(formChecker.checkPassword);
		$('#form_signup [name=password_confirmed]').focusout(formChecker.checkRepassword);
		$('#form_signup [name=captcha]').focusout(formChecker.checkCaptcha);
		$('#form_signup [name=agree]').click(function() {
			if ($(this).hasClass('checked'))
				$(this).removeClass('checked');
			else
				$(this).addClass('checked');
		});

		$('#change_captha_img').click(
				function() {
					$('#captha_img').attr('src',
							$("#webroot").val() + '/captcha_signup.jpg?' + Math.floor(Math.random() * 100));
				});

		$.ajax({
			url : getRootPath() + '/customerajax/findCaptha!findCaptha.action',
			cache : false,
			async : false,
			type : "POST",
			dataType : 'json',
			success : function(result) {
				theCaptcha = result.captha;
			}
		});

		$('#form_signup .submit').click(function() {
			$('#form_signup input').focusout();
			if ($('#form_signup .checker_ok').length == 5 && $('#form_signup [name=agree]').hasClass('checked')) {
				$('#form_signup').submit();
			}
		});
	},
	checkName : function() {
		$('#showErrorCode').html('注册只需<em>30秒</em>，注册成功即可获得<em>70积分</em>哦~~');
		var name = $('#form_signup [name=username]').val();
		var nameReg = /^[a-zA-Z0-9_]+$/;
		if (name == '' || name == $('#form_signup [name=username]').attr('tips')) {
			$('#form_signup [name=checker_name]').removeClass().addClass('checker_notice');
			$('#form_signup [name=checker_name] span').html('请输入您的用户名');
		} else if (nameReg.test(name)) {
			$('#form_signup [name=checker_name]').removeClass().addClass('checker_ok');
			$('#form_signup [name=checker_name] span').html('');
		} else {
			$('#form_signup [name=checker_name]').removeClass().addClass('checker_wrong');
			$('#form_signup [name=checker_name] span').html('只能包含英文、数字、下划线');
		}
		var username = $('#form_signup [name=username]:not(.inputTips)').val();
		$.ajax({
			url : getRootPath() + "/customerajax/signup!checkName.action?username=" + username,
			cache : false,
			async : false,
			type : "POST",
			dataType : 'json',
			success : function(data) {
				if (data.errorCode != "") {
					$('#form_signup [name=checker_name]').removeClass().addClass('checker_notice');
					$('#form_signup [name=checker_name] span').html(data.errorCode);
				}
			}
		});
	},
	checkEmail : function() {
		$('#showErrorCode').html('注册只需<em>30秒</em>，注册成功即可获得<em>70积分</em>哦~~');
		var email = $('#form_signup [name=email]').val();
		var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if (email == '' || email == $('#form_signup [name=email]').attr('tips')) {
			$('#form_signup [name=checker_email]').removeClass().addClass('checker_notice');
			$('#form_signup [name=checker_email] span').html('请输入您的邮箱');
		} else if (emailReg.test(email)) {
			$('#form_signup [name=checker_email]').removeClass().addClass('checker_ok');
			$('#form_signup [name=checker_email] span').html('');
		} else {
			$('#form_signup [name=checker_email]').removeClass().addClass('checker_wrong');
			$('#form_signup [name=checker_email] span').html('请输入正确的邮箱地址');
		}
		var email = $('#form_signup [name=email]:not(.inputTips)').val();
		$.ajax({
			url : getRootPath() + "/customerajax/signup!checkEmail.action?email=" + email,
			cache : false,
			async : false,
			type : "POST",
			dataType : 'json',
			success : function(data) {
				if (data.errorCode != "") {
					$('#form_signup [name=checker_email]').removeClass().addClass('checker_notice');
					$('#form_signup [name=checker_email] span').html(data.errorCode);
				}
			}
		});
	},
	checkPassword : function() {
		$('#showErrorCode').html('注册只需<em>30秒</em>，注册成功即可获得<em>70积分</em>哦~~');
		var pass = $('#form_signup [name=password]').val();
		var repass = $('#form_signup [name=repassword]').val();
		if (pass == '') {
			$('#form_signup [name=checker_password]').removeClass().addClass('checker_notice');
			$('#form_signup [name=checker_password] span').html('请输入您的密码');
		} else if (pass.length < 5) {
			$('#form_signup [name=checker_password]').removeClass().addClass('checker_wrong');
			$('#form_signup [name=checker_password] span').html('密码至少为6位');
		} else if (repass != '' && pass != repass) {
			$('#form_signup [name=checker_password]').removeClass().addClass('checker_wrong');
			$('#form_signup [name=checker_password] span').html('您两次输入的密码不同');
		} else {
			$('#form_signup [name=checker_password]').removeClass().addClass('checker_ok');
			$('#form_signup [name=checker_password] span').html('');
		}
	},
	checkRepassword : function() {
		$('#showErrorCode').html('注册只需<em>30秒</em>，注册成功即可获得<em>70积分</em>哦~~');
		var pass = $('#form_signup [name=password]').val();
		var repass = $('#form_signup [name=repassword]').val();
		if (pass == '' && repass == '') {
			$('#form_signup [name=checker_repassword]').removeClass();
			$('#form_signup [name=checker_repassword] span').html('');
		} else if (pass != '' && repass == '') {
			$('#form_signup [name=checker_repassword]').removeClass().addClass('checker_notice');
			$('#form_signup [name=checker_repassword] span').html('请再次输入您的密码');
		} else if (pass == repass) {
			$('#form_signup [name=checker_repassword]').removeClass().addClass('checker_ok');
			$('#form_signup [name=checker_repassword] span').html('');
		} else {
			$('#form_signup [name=checker_repassword]').removeClass().addClass('checker_wrong');
			$('#form_signup [name=checker_repassword] span').html('您两次输入的密码不同');
		}
	},
	checkCaptcha : function() {
		$.ajax({
			url : getRootPath() + '/customerajax/findCaptha!findCaptha.action',
			cache : false,
			async : false,
			type : "POST",
			dataType : 'json',
			success : function(result) {
				theCaptcha = result.captha;
			}
		});
		var captcha = $('#form_signup [name=captcha]').val();
		if (captcha.toLowerCase() == theCaptcha.toLowerCase()) {
			$('#form_signup [name=checker_captcha]').removeClass().addClass('checker_ok');
			$('#form_signup [name=checker_captcha] span').html('');
		} else {
			$('#form_signup [name=checker_captcha]').removeClass().addClass('checker_wrong');
			$('#form_signup [name=checker_captcha] span').html('请输入上方图片中的文字');
		}
	}
};
